package com.woniuxy.admin.controller;


import com.woniuxy.admin.dto.MenuDto;
import com.woniuxy.admin.entity.AdminRole;
import com.woniuxy.admin.entity.Menu;
import com.woniuxy.admin.service.AdminRoleService;
import com.woniuxy.admin.service.MenuService;

import com.woniuxy.admin.util.JWTUtil;
import com.woniuxy.admin.util.TokenUtil;
import com.woniuxy.core.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ...
 * @since 2024年05月22日
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "权限菜单")
public class MenuController {
    @Resource
    private MenuService menuService;
    @Resource
    private AdminRoleService adminRoleService;
    @Resource
    private TokenUtil tokenUtil;

    /**
     * 查询权限菜单列表以及菜单的子节点
     * @return
     */
     @GetMapping("/loadAllMenu")
     @ApiOperation("查询权限菜单及子菜单")
     @ApiResponses({
             @ApiResponse(code = 200,message = "OK",response = MenuDto.class)
     })
     public Result loadAllMenu(){
     List<MenuDto>dto= menuService.loadAllMenu();
     return Result.success("查询权限菜单成功",dto);
    }


    /**
     * 查询权限菜单列表以及菜单的子节点
     * @return
     */
    @GetMapping("/loadAllMenuBy")
    @ApiOperation("根据角色Id 查询权限菜单及子菜单")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = Menu.class)
    })
    public Result loadAllMenuByRoleId(@RequestHeader  String token){
        Integer accountId = tokenUtil.getAccountId(token);
        AdminRole adminRole = adminRoleService.loadAllRoleByAccountId(accountId);
        List<MenuDto>dto= menuService.loadAllMenuByRoleId(adminRole.getRoleId());
        return Result.success("查询权限菜单成功",dto);
    }







}

